package com.example.wanghao.wangyixinwen.fragment;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.wanghao.wangyixinwen.R;
import com.example.wanghao.wangyixinwen.act.MainActivity;
import com.example.wanghao.wangyixinwen.adapter.MyBaseAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by wanghao on 2016/5/19.
 */
public class MenuFragment2 extends BaseFragment implements AdapterView.OnItemClickListener {


    public static final int NEWCENTER = 0;//新闻中心状态
    public static final int SMARTSERVICE = 1;//智慧服务状态
    public static final int GOVER = 2;//服务指南状态

    private int menuType = NEWCENTER;//记录当前是显示哪一个页面的才到那列表

    @Bind(R.id.lv_menu_news_center)
    ListView mLvMenuNewsCenter;
    @Bind(R.id.lv_menu_smart_service)
    ListView mLvMenuSmartService;
    @Bind(R.id.lv_menu_govaffairs)
    ListView mLvMenuGovaffairs;
    private MenuAdapter mMenuAdapter;
    private MenuAdapter mGoverAdapter;
    private MenuAdapter mSmartAdapter;


    @Override
    public View initView(LayoutInflater inflater) {
        View view = View.inflate(mContext, R.layout.layout_left_menu, null);
        //ViewUtils.inject(this, view);
        return view;
    }

    @Override
    protected void initData() {

        mLvMenuNewsCenter.setOnItemClickListener(this);
        mLvMenuSmartService.setOnItemClickListener(this);
        mLvMenuGovaffairs.setOnItemClickListener(this);
    }

    //新闻中心标题数据集合
    private List<String> mNewCenterTitles = new ArrayList<>();

    public void initMenu(List<String> newcenterTitles) {

        mNewCenterTitles.clear();
        mNewCenterTitles.addAll(newcenterTitles);

        mMenuAdapter = new MenuAdapter(mNewCenterTitles,mContext);
        mLvMenuNewsCenter.setAdapter(mMenuAdapter);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()){
            case R.id.lv_menu_news_center:
                //让对应点击事件的条目设置选中状态
                mMenuAdapter.setClickItemPosition(position);

                //控制新闻中心进行界面切换
              /*  NewCenterPage newCenterPage = new NewCenterPage(mContext);
                newCenterPage.switchPage(position);*/

                ((MainActivity)mContext).getHomeFragment().getNewCenter().switchPage(position);
                break;
            case R.id.lv_menu_smart_service:
                mSmartAdapter.setClickItemPosition(position);
                break;
            case R.id.lv_menu_govaffairs:
                mGoverAdapter.setClickItemPosition(position);
                break;
        }

        mSlidingMenu.toggle();
    }

    //设置列表显示类型
    public void setMenuType(int type) {
        mLvMenuNewsCenter.setVisibility(View.GONE);
        mLvMenuSmartService.setVisibility(View.GONE);
        mLvMenuGovaffairs.setVisibility(View.GONE);
        this.menuType = type;
        switch (menuType){
            case NEWCENTER:
                mLvMenuNewsCenter.setVisibility(View.VISIBLE);
                if (mMenuAdapter != null){
                    mMenuAdapter.notifyDataSetChanged();
                }
                break;
            case SMARTSERVICE:
                mLvMenuSmartService.setVisibility(View.VISIBLE);
                List<String> smartTitles = new ArrayList<>();
                smartTitles.add("智慧服务1");
                smartTitles.add("智慧服务2");
                smartTitles.add("智慧服务3");
                mSmartAdapter = new MenuAdapter(smartTitles,mContext);
                mLvMenuSmartService.setAdapter(mSmartAdapter);
                break;
            case GOVER:
                mLvMenuGovaffairs.setVisibility(View.VISIBLE);
                List<String> goverTitles = new ArrayList<>();
                goverTitles.add("政务指南1");
                goverTitles.add("政务指南2");
                goverTitles.add("政务指南3");
                mGoverAdapter = new MenuAdapter(goverTitles,mContext);
                mLvMenuGovaffairs.setAdapter(mGoverAdapter);
                break;
        }

    }

   /* public void initSmartMenu(List<String> smartTitles) {
        mLvMenuNewsCenter.setVisibility(View.GONE);
        mLvMenuGovaffairs.setVisibility(View.GONE);
        mLvMenuSmartService.setVisibility(View.VISIBLE);
        MenuAdapter smartAdapter = new MenuAdapter(smartTitles,mContext);
        mLvMenuSmartService.setAdapter(smartAdapter);
    }*/


    private class MenuAdapter extends MyBaseAdapter<String> {

        private int mClickPosition;//对应选中的条目位置

        public MenuAdapter(List<String> datas, Context context) {
            super(datas, context);
        }

       /* @Override
        public int getCount() {
            return mNewCenterTitles.size();
        }

        @Override
        public Object getItem(int position) {
            return mNewCenterTitles.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
*/
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.layout_item_menu, null);
            }

            ImageView iv = (ImageView) convertView.findViewById(R.id.iv_menu_item);
            TextView tv = (TextView) convertView.findViewById(R.id.tv_menu_item);

           // tv.setText(mNewCenterTitles.get(position));
            tv.setText(mDatas.get(position));

            if (position == mClickPosition){//如果是选中位置,则设置为选中状态
                tv.setTextColor(Color.RED);
                iv.setImageResource(R.drawable.menu_arr_select);
                convertView.setBackgroundResource(R.drawable.menu_item_bg_select);
            }else{
                tv.setTextColor(Color.WHITE);
                iv.setImageResource(R.drawable.menu_arr_normal);
                convertView.setBackgroundResource(android.R.color.transparent);
            }
            return convertView;
        }

        public void setClickItemPosition(int position) {
            this.mClickPosition = position;
            notifyDataSetChanged();//刷新
        }
    }

}
